/** 
 * Copyright(C) 2010-2011 <webPrj> LTD. All Rights Reserved.                               
 * Project: webPrj
 * Module ID: <0001>
 * Comments: TODO         
 * JDK version used: <JDK1.6.0_25>                              
 * Author: xiamingxing
 * Create Date：10:39:55
 * Modified By: xiamingxing                              
 * Modified Date: 10:39:55                                  
 * Why & What is modified: <修改原因描述>    
 * Version: V1.0                  
 */
package com.xiamx.platform.core.dao.impl;

import java.io.Serializable;
import java.sql.SQLException;
import java.util.List;

import org.springframework.orm.ibatis.SqlMapClientCallback;
import org.springframework.orm.ibatis.SqlMapClientTemplate;

import com.ibatis.sqlmap.client.SqlMapExecutor;
import com.xiamx.platform.core.dao.IBaseDao;

public class BaseDao<T, PK extends Serializable> implements IBaseDao<T, PK> {

	private SqlMapClientTemplate sqlMapClientTemplate;
	
	@Override
	public void insert(String statementName, T entity) throws Exception {
		try {
			sqlMapClientTemplate.insert(statementName, entity);
		} catch (Exception e) {
			throw e;
		}
	}

	@Override
	public int update(String statementName, T entity) throws Exception{
		int res = 0;
		try {
			res = sqlMapClientTemplate.update(statementName, entity);
		} catch (Exception e) {
			throw e;
		}
		return res;
	}

	@Override
	public int delete(String statementName, T entity) throws Exception{
		int res = 0;
		try {
			res = sqlMapClientTemplate.delete(statementName, entity);
		} catch (Exception e) {
			throw e;
		}
		return res;
	}

	@Override
	public int delete(String statementName, PK primaryKey) throws Exception{
		int res = 0;
		try {
			res = sqlMapClientTemplate.delete(statementName, primaryKey);
		} catch (Exception e) {
			throw e;
		}
		return res;
	}

	@Override
	public int delete(String statementName) throws Exception{
		int res = 0;
		try {
			res = sqlMapClientTemplate.delete(statementName);
		} catch (Exception e) {
			throw e;
		}
		return res;
	}
	
	@Override
	public int logicDelete(String statementName, PK primaryKey) throws Exception{
		int res = 0;
		try {
			res = sqlMapClientTemplate.update(statementName, primaryKey);
		} catch (Exception e) {
			throw e;
		}
		return res;
	}

	@Override
	public int logicDelete(String statementName, T param) throws Exception{
		int res = 0;
		try {
			res = sqlMapClientTemplate.delete(statementName, param);
		} catch (Exception e) {
			throw e;
		}
		return res;
	}

	@Override
	public int truncate(String statementName) throws Exception{
		int res = 0;
		try {
			res = sqlMapClientTemplate.delete(statementName);
		} catch (Exception e) {
			throw e;
		}
		return res;
	}

	@Override
	public Integer count(String statementName) throws Exception{
		int res = 0;
		try {
			res = sqlMapClientTemplate.queryForList(statementName).size();
		} catch (Exception e) {
			throw e;
		}
		return res;
	}

	@Override
	public Integer count(String statementName, T param) throws Exception{
		int res = 0;
		try {
			res = sqlMapClientTemplate.queryForList(statementName, param).size();
		} catch (Exception e) {
			throw e;
		}
		return res;
	}

	@SuppressWarnings("unchecked")
	@Override
	public T get(String statementName, T t) throws Exception{
		T tt = null;
		try {
			tt = (T) sqlMapClientTemplate.queryForObject(statementName, t);
		} catch (Exception e) {
				throw e;
		}
		return tt;
	}

	@SuppressWarnings("unchecked")
	@Override
	public T load(String statementName, PK primaryKey) throws Exception {
		T tt = null;
		try {
			tt = (T) sqlMapClientTemplate.queryForObject(statementName, primaryKey);
		} catch (Exception e) {
				throw e;
		}
		return tt;
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<T> select(String statementName) throws Exception{
		List<T> tt = null;
		try {
			tt = sqlMapClientTemplate.queryForList(statementName);
		} catch (Exception e) {
				throw e;
		}
		return tt;
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<T> select(String statementName, T param) throws Exception{
		List<T> tt = null;
		try {
			tt = sqlMapClientTemplate.queryForList(statementName, param);
		} catch (Exception e) {
				throw e;
		}
		return tt;
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<T> select(String statementName, T param, int rowStart,
			int pageSize) throws Exception{
		List<T> tt = null;
		try {
			tt = sqlMapClientTemplate.queryForList(statementName, param, rowStart, pageSize);
		} catch (Exception e) {
				throw e;
		}
		return tt;
	}

	@SuppressWarnings({ "rawtypes", "unchecked" })
	@Override
	public void batchInsert(final String statementName, final List<T> list) throws Exception{
		SqlMapClientCallback callback = new SqlMapClientCallback() 
		{
		    public Object doInSqlMapClient(SqlMapExecutor executor)throws SQLException 
			{
				executor.startBatch();
				for (T member : list) 
				{
				    executor.insert(statementName, member);
				}
				executor.executeBatch();
				return null;
		    }
		};
		sqlMapClientTemplate.execute(callback);
	}

	@SuppressWarnings({ "unchecked", "rawtypes" })
	@Override
	public void batchUpdate(final String statementName, final List<T> list) throws Exception{
		SqlMapClientCallback callback = new SqlMapClientCallback() {
		    public Object doInSqlMapClient(SqlMapExecutor executor)
			    throws SQLException {
			executor.startBatch();
			for (T member : list) {
			    executor.update(statementName,member);
			}
			executor.executeBatch();
			return null;
		    }
		};
		try {
			sqlMapClientTemplate.execute(callback);
		} catch (Exception e) {
			throw e;
		}
	}

	@SuppressWarnings({ "rawtypes", "unchecked" })
	@Override
	public void batchDelete(final String statementName, final List<PK> list) throws Exception{
		SqlMapClientCallback callback = new SqlMapClientCallback() {
		    public Object doInSqlMapClient(SqlMapExecutor executor)
			    throws SQLException {
			executor.startBatch();
			for (PK member : list) {
			    executor.delete(statementName,member);
			}
			executor.executeBatch();
			return null;
		    }
		};
		try {
			sqlMapClientTemplate.execute(callback);
		} catch (Exception e) {
			throw e;
		}
	}

	@SuppressWarnings({ "rawtypes", "unchecked" })
	@Override
	public void batchLogicDelete(final String statementName, final List<PK> list) throws Exception{
		SqlMapClientCallback callback = new SqlMapClientCallback() {
		    public Object doInSqlMapClient(SqlMapExecutor executor)
			    throws SQLException {
			executor.startBatch();
			for (PK member : list) {
			    executor.update(statementName ,member);
			}
			executor.executeBatch();
			return null;
		    }
		};
		try {
			sqlMapClientTemplate.execute(callback);
		} catch (Exception e) {
			throw e;
		}
	}

	@Override
	public SqlMapClientTemplate getSqlMapClientTemplate(){
		return sqlMapClientTemplate;
	}
	
	public void setSqlMapClientTemplate(SqlMapClientTemplate sqlMapClientTemplate){
		this.sqlMapClientTemplate = sqlMapClientTemplate;
	}

}

